package com.aiju.cash.ui.member;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.MemberApi;
import com.aiju.cash.ui.member.entity.Member;
import com.aiju.cash.ui.member.entity.MemberCard;
import com.aiju.cash.widgit.MemberCardVew;
import com.aiju.common.PixValue;
import com.aiju.utils.T;
import com.aiju.widgit.layout.SettingItemTextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2017-02-20 15:59
 * @version: V1.0
 * @Description: 会员卡领取
 */
public class MemberCardUserActivity extends BaseTitleActivity {

    private TextView textView;
    private ListView mListView;

    private MemberCard memberCard;
    private MemberCardVew memberCardVew;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_member_card_user);
        setCenterTitle("会员卡领取名单");

        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.mipmap.icon_search_button);
        mAiJuTitleBarView.setCustomizedRightView(imageView);
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goMemberSearchActivity();
            }
        });
        memberCard = (MemberCard) getIntent().getSerializableExtra("memberCard");

        textView = (TextView) findViewById(R.id.tv_card_user_num);
        mListView = (ListView) findViewById(R.id.lv_card_user);
        memberCardVew = (MemberCardVew) findViewById(R.id.card_user);
        memberCardVew.setCardName(memberCard.getName());
        memberCardVew.setCardDiscount(memberCard.getDiscount());
        if (memberCard.isForever()) {
            memberCardVew.showForever();
        } else {
            memberCardVew.setCardTime(memberCard.getTime());
        }
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                goMemberInfo(position);
            }
        });
        requsetMemberCardId();
    }

    /**
     * 会员详情
     */
    private void goMemberInfo(int position) {
        Intent intent = new Intent(this, MemberInfoCardsActivity.class);
        intent.putExtra("memberId",members.get(position).getId());
        startActivity(intent);
    }

    private List<Member> members;

    private void requsetMemberCardId() {
        mDialogHelper.showLoadingProgressDialog();
        new MemberApi().getByMCardId(memberCard.getId(), mUser.getToken(), "1", "10", new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                mDialogHelper.dismissProgressDialog();
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                mDialogHelper.dismissProgressDialog();
                DataJson dataJson = new DataJson(response);
                if (dataJson.isSuccess()) {
                    members = new Gson().fromJson(dataJson.getData().toString(), new TypeToken<List<Member>>() {
                    }.getType());
                    mListView.setAdapter(new MyBaseAdapter(MemberCardUserActivity.this, members));
                    textView.setText("共" + members.size() + "人领取");
                } else {
                    T.ToastShort(dataJson.getMsg());
                }
            }
        });
    }


    /**
     * 跳转搜索会员
     */
    private void goMemberSearchActivity() {
        Intent intent=new Intent(this,MemberSearchActivity.class);
        intent.putExtra("memberCardId",memberCard.getId());
        startActivity(intent);
    }

    class MyBaseAdapter extends BaseAdapter {

        private List<Member> cards;

        private Context context;

        public MyBaseAdapter(Context context, List<Member> cards) {
            this.context = context;
            this.cards = cards;
        }

        @Override
        public int getCount() {
            return cards.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Member member = cards.get(position);
            SettingItemTextView textView = new SettingItemTextView(context);
            textView.getRightView().setTextColor(Color.parseColor("#666666"));
            textView.setLeftText(member.getName());
            textView.setRightText(member.getPhone());
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, PixValue.dp.valueOf(44)));
            return textView;
        }
    }
}
